package com.zth.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.zth.pojo.Blog;
import com.zth.pojo.Tag;
import com.zth.service.BlogServace;
import com.zth.util.FileUtil;

@RestController
@RequestMapping("/blog")
public class BlogController {
	@Autowired
	private BlogServace blogServace;
	

	private String url;
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@RequestMapping("/blogAll")
	public Map<String, Object> blogAll(@RequestParam(defaultValue = "1", name = "page") int page,
			@RequestParam(defaultValue = "10", name = "limit") int pageSize, Blog blog) {
		List<Blog> list = blogServace.blogAll(page, pageSize, blog);
		PageInfo<Blog> info = new PageInfo<>(list);
		Map<String, Object> map = new HashMap<>();

		map.put("code", 0);
		map.put("msg", "");
		map.put("count", info.getTotal());
		map.put("data", info.getList());
		return map;
	}

	@RequestMapping("/deleteBatch")
	public int deleteBatch(@RequestParam(name = "ids[]") int[] ids) {
		return blogServace.deleteBatch(ids);
	}

	@RequestMapping("/blogById")
	public Blog blogById(int id) {
		return blogServace.blogById(id);
	}

	@RequestMapping("/edit")
	public Map<String, Object> edit(Blog blog) {
		
		if(blog.getBlog_tags()=="undefined" || blog.getBlog_tags().toString().equals("undefined")) {
			blog.setBlog_tags(null);
		}
		System.out.println(blog.getBlog_tags());
		if (getUrl() != null && !(getUrl().equals(""))) {
			blog.setBlog_cover_image(getUrl());
		}
		LocalDateTime time = LocalDateTime.now();
		blog.setUpdate_time(time);
		int row = blogServace.edit(blog);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", "编辑");
		map.put("row", row);
		return map;
	}

	@RequestMapping("/add")
	public Map<String, Object> add(Blog blog) {
		LocalDateTime time = LocalDateTime.now();
		blog.setCreate_time(time);
		System.out.println(getUrl());
		if (getUrl() != null && !(getUrl().equals(""))) {
			blog.setBlog_cover_image(getUrl());
		} else {
			blog.setBlog_cover_image("/ssb/uploads/d7c90553-ba06-44f7-971a-a8cd039d6dc9.jpg");
		}
		
		int row = blogServace.add(blog);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", "添加");
		map.put("row", row);
		return map;
	}

	@RequestMapping("/getTag")
	public String getTag(@RequestParam(name = "blog_tags[]") int[] blog_tags) {
		List<String> blog_tag = blogServace.getTag(blog_tags);
		String blog_tagl = String.join(",", blog_tag);
		return blog_tagl;
	}
	
	// 普通图片上传
	@RequestMapping("/common_upload")
	public Map<String, Object> common_upload(MultipartFile file){
		String files = FileUtil.getFileuploading(file, "D:/uploads/");
		setUrl("/ssb"+files);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message ", "");
		map.put("url", "/ssb" + files);
		return map;
	}

	
	//添加阅读量
	@RequestMapping("editViews")
	public int editViews(Blog blog){
		return blogServace.editViews(blog);
	}
	
	// 富文本图片上传
		@RequestMapping("/upload")
		public Map<String, Object> upload(
				@RequestParam(value = "editormd-image-file", required = true) MultipartFile file) {
			String files = FileUtil.getFileuploading(file, "D:/uploads/");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("success", 1);
			map.put("message ", "");
			map.put("url", "/ssb" + files);

			return map;
		}

}
